package com.platform.service;

import com.platform.dao.ApiTbGoodsMapper;
import com.platform.entity.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApiTbGoodsService {

    @Autowired
    private ApiTbGoodsMapper apiTbGoodsMapper;

    @Autowired
    private ApiGoodsIssueService goodsIssueService;
    @Autowired
    private ApiGoodsGalleryService goodsGalleryService;

    public TbGoodsVo getTbGoodsVoById(int id){
        return apiTbGoodsMapper.getTbGoodsVoById(id);
    }

    public Map<String,Object> getTbGoodsVo(int id){
        Map<String,Object> map=new HashedMap();
        List<GoodsIssueVo> issue = goodsIssueService.queryList(null);
        TbGoodsVo good=apiTbGoodsMapper.getTbGoodsVoById(id);
        if(good.getIs_combo()==1){
            List<TbGoodsVo> list=apiTbGoodsMapper.querySpeList(id);
            map.put("speGoods",list); //套餐下辖商品列表
        }
        Map param = new HashMap();
        param.put("goods_id", id);
        List<GoodsGalleryVo> gallery = goodsGalleryService.queryList(param);
        map.put("issue",issue); //常见问题
        map.put("good",good);  //商品信息
        map.put("gallery",gallery);  //商品图片

        return map;
    }

    public TbGoodsVo getTbGoodsVoByNum(String num){
        return apiTbGoodsMapper.getTbGoodsVoByNum(num);
    }

    public List<TbGoodsVo> queryList(Integer category_id,String name,Integer is_sale,Integer is_hot,Integer pageIndex,Integer pageSize){
        Map<String,Object> map=new HashedMap();
        map.put("category_id",category_id);
        map.put("name",name);
        map.put("is_sale",is_sale);
        map.put("is_hot",is_hot);
        map.put("offset",(pageIndex-1)*pageSize);
        map.put("limit",pageSize);
        return apiTbGoodsMapper.queryList(map);
    }

    public List<TbGoodsSpeVo> queryGoodsSpe(Integer id){
        return apiTbGoodsMapper.queryGoodsSpe(id);
    }

    /**
     * 获取指定id的商品的所有规格，返回map形式，用于加入购物车和生成订单时对商品信息的校验
     * @param id
     * @return
     */
    public Map<Integer,TbGoodsFixingsVo> queryGoodsFix(Integer id){
        Map<Integer,TbGoodsFixingsVo> map=new HashMap<>();
        List<TbGoodsFixingsVo> list=apiTbGoodsMapper.queryGoodsFixings(id);
        for(int i=0;i<list.size();i++){
            map.put(list.get(i).getId(),list.get(i));
        }
        return map;
    }

    public List<Map<String, Object>> queryRelatedList(String[] categoryIds, List<Integer> ids){
        return apiTbGoodsMapper.queryRelatedList(categoryIds, ids);
    }

}
